<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<?php $this->load->view('head'); ?>
</head>

<body>
	<?php if ($user->level > 0): ?>
	<?php $this->load->view('header_admin.php'); ?>
	
	<script type="text/javascript">
		function editNew(editId) {
			var newInfo = eval('(' + jQuery("input#newInfo_" + editId).val() + ')');
			
			jQuery("input#title").val(newInfo.title);
			jQuery("textarea#content").val(newInfo.content);
			jQuery("input#editId").val(editId);
			
			jQuery("div#addBtnCont").css("display", "none");
			jQuery("div#editBtnCont").css("display", "block");
		}
		
		function cancelNewEdit() {
			jQuery("input#title, textarea#content, input#editId").val("");
			
			jQuery("div#addBtnCont").css("display", "block");
			jQuery("div#editBtnCont").css("display", "none");
		}
	</script>
	<?php else: ?>
	<?php if ($user->id > 0): ?>
	<?php $this->load->view('header_logged.php'); ?>
	<?php else: ?>
	<?php $this->load->view('header_guest.php'); ?>
	<?php endif; ?>
	<?php endif; ?>
	
	<!-- Content Area -->
	<div class="wrapper-world content">
		<?php if ($user->level > 0): ?>
		<div class="wrapper">
			<h1>Submit news</h1>
			
			<?php echo form_open('/news/post', 'class="left-aligned" id="addNews"'); ?>
				<div class="one">
					<label for="title">Title:</label>
					<div class="type-text"><input type="text" name="title" id="title" /></div>
					<div class="clr"></div>
				</div>
				<div class="one">
					<label for="content">Text:</label>
					<div class="type-textarea"><textarea name="content" id="content"></textarea></div>
					<div class="clr"></div>
				</div>
				<input type="hidden" name="editId" id="editId" value="" />
			</form>
		</div>
		
		<div class="wrapper-world add-btn">
			<div class="wrapper" id="addBtnCont">
				<a href="javascript:jQuery('form#addNews').submit();" title="Add this new">ADD</a>
				<div class="clr"></div>
			</div>
			
			<div class="wrapper" id="editBtnCont" style="display:none;">
				<a href="javascript:jQuery('form#addNews').submit();" title="Edit this new" class="apply-btn">APPLY</a> <a href="javascript:cancelNewEdit();" title="Cancel edit" class="cancel-btn">CANCEL</a>
				<div class="clr"></div>
			</div>
		</div>
		<?php endif; ?>
		
		<div class="wrapper">
			<?php foreach ($news as $new): ?>
			<h1><span class="quantity"><?php echo date("j M Y", $new->posted); ?></span> <?php echo htmlspecialchars($new->title); ?><?php if ($user->level > 0): ?><input type="hidden" name="newInfo" id="newInfo_<?php echo $new->id; ?>" value='<?php echo json_encode($new); ?>' /> <span class="quantity link"><a href="javascript:editNew(<?php echo $new->id; ?>);">Edit</a></span> <span class="quantity link"><?php echo anchor('/news/delete/'.$new->id, 'Delete'); ?></span><?php endif; ?></h1>
			<div class="wrapper">
				<p><?php echo nl2br(htmlspecialchars($new->content)); ?></p>
			</div>
			<?php endforeach; ?>
		</div>
	</div> <!-- end of wrapper-world and .content -->
	
	
		
	<div class="wrapper-world footer-top">
		<div class="wrapper"></div>	
	</div>
	
	<?php $this->load->view('footer'); ?>
</body>
</html>
